package im.starchat.chat.activity.profile;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.activity.BaseActivity;
import im.starchat.chat.rest.model.UserBean;
import im.starchat.chat.utils.GsonUtil;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.utils.UserUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 修改简介描述
 */
public class EditUserDescribeActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.apply_back)
    ImageView mBackBtn;
    @InjectView(R.id.describe)
    EditText mDescribeEdt;
    @InjectView(R.id.apply_btn)
    Button mApplyBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_describe);
        ButterKnife.inject(this);
        mBackBtn.setOnClickListener(this);
        mApplyBtn.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.apply_back:
                finish();
                break;
            case R.id.apply_btn:
                apply();
                break;
        }
    }

    public void apply(){
        showProgressDialog();
        if(!TextUtils.isEmpty(mDescribeEdt.getText().toString())){
            XLApplication.getInstance().getRestClient().putUserInfoDescription(PreferencesStorageUtil.getUserId(EditUserDescribeActivity.this), mDescribeEdt.getText().toString(),new Callback<UserBean>() {
                @Override
                public void success(UserBean userBean, Response response) {
                    PreferencesStorageUtil.setUserBean(EditUserDescribeActivity.this, GsonUtil.toJson(userBean));
                    UserUtils.setUserInstance(userBean);
                    toast("修改成功!", Toast.LENGTH_SHORT);
                    mDescribeEdt.setText("");
                    dismissProgressDialog();
//                    PreferencesStorageUtil.setStar(EditUserNameActivity.this, true);
                    finish();
                }

                @Override
                public void failure(RetrofitError error) {
                    dismissProgressDialog();
                    toast("修改失败", Toast.LENGTH_SHORT);
                }
            });
        }else{
            toast("请输入个性签名", Toast.LENGTH_SHORT);
        }
    }

}
